<?php

/**
 * Clase para acceder y manipular los datos de las paginas
 * @author Alberto Berroteran
 */
final class Post extends ARecord{

	/**
	 *
	 */
	function add(){
		$this->cols('title', 'content', 'is_show', 'allowc')->tables('post')->setValue("allowc:$i_allowc");
		return $o_db->insert();
	}

	function edit($i_id){
		$o_db  = new dbARecord($this->o_connect);
		$o_dbpost->cols('title', 'content', 'is_show', 'allowc')->tables('post')->setValue("allowc:$i_allowc");
		return $o_dbpost->update('id_post', $i_id);
	}

	function getPosts(){
		$this->tables('post');
		$this->cols('title', 'id_post', 'content', 'modified, UNIX_TIMESTAMP(modified) as unixt');
		$this->where('is_show = 0')->order('unixt DESC')->limit(10);
		return $this->select();
	}

	function getPost($s_id){
		$this->tables('post')->where('title = "%.35s"', $s_id)->cols('*');
		return $this->select_one();
	}

	function getById($i_id){
		$this->tables('post')->where('id_post = %u', $i_id)->cols('*');
		return $this->select_one();
	}
	
	function getComments($i_id){
		$this->tables('comment')->cols('*')->where('id_com=%u',$i_id);
		return $this->select();
	}

	function getTags($i_idpost){
		$this->tables('tag', 'tagpost')->cols('name');
		$this->where('#@tagpost.id_post = %u AND #@tagpost.id_tag = #@tag.id_tag', $i_idpost);
		return ABApi::obj2array($this->select(), 'name');
	}

	function getPostByCategory($s_cat){
		$this->tables('category', 'post')->cols('title', 'SUBSTRING(content,1,200) as content');
		$this->where('name="%.20s"  AND category = id_cat', $s_cat)->order('modified DESC');
		return $this->select();
	}
	
	function toindex($i_limit  = 50, $i_init = 0){
		$o_db->tables('post')->cols('id_post', 'title', 'modified', 'is_show');
		$o_db->limit($i_limit, $i_init)->order('modified DESC');
		return $o_db->select();
	}
	
	function delete($i_id){
		$o_db = new dbARecord($this->o_connect);
		$o_db->tables('post')->limit(1);
		return $o_db->delete('id_post', $i_id);
	}
	
	function addTags($i_idpost, $s_tags){
		$o_db = new dbARecord($this->o_connect);
		#Extraigo todos los tag
		$a_tags = $o_db->cols('name')->tables('tag')->select();
		$cb_func = create_function('$o_val', 'return $o_val->name;');
		$a_tags = array_map($cb_func, $a_tags);
		#Parto los tag
		$a_ptags = explode(',', $s_tags);
		$a_ptags = array_map('trim', $a_ptags);
		$a_ptags = array_map('strtolower', $a_ptags);
		#Inserta tag nuevos
		$a_diff = array_udiff($a_ptags, $a_tags,  'strcasecmp');
		foreach ($a_diff as $s_value){
			$o_db->setValue("name:$s_value");
			$o_db->insert();
		}
		//borra viejas asociaciones si existen
		$o_dbpost->tables('tagpost')->delete('id_post', $i_idpost);
		//asociacion de tags con la pagina
		$a_atags = $o_dbpost->cols('id_tag', 'name')->tables('tag')->select();
		$o_dbpost->tables('tagpost')->cols('id_post', 'id_tag');
		foreach($a_atags as $o_tag){
			if(!in_array(strtolower($o_tag->name), $a_ptags, false))continue;
			$o_dbpost->setValue("id_post:$i_idpost", "id_tag:{$o_tag->id_tag}");
			$o_dbpost->insert();
		}
	}
}//end of class
?>
